<?php
//dezend by http://www.yunlu99.com/ QQ:270656184
defined('BASEPATH') || exit('No direct script access allowed');
class Webmasters extends MY_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->verify_user();
	}

	public function index()
	{
		$this->view('webmasters');
	}

	public function data()
	{
		$page = ($this->input->post('page') ? intval($this->input->post('page')) : 1);
		$rows = ($this->input->post('rows') ? intval($this->input->post('rows')) : 10);
		$result['total'] = $this->db->count_all_results('webmasters');
		$start = ($page - 1) * $rows;
		$this->db->limit($rows, $start);
		$this->db->select();
		$rs = $this->db->get('webmasters');
		$result['rows'] = $rs->result_array();
		echo json_encode($result);
	}

	public function add()
	{
		$d['power'] = $this->d->rows(array('table' => 'power'));
		$this->load->view('webmasters_add', $d);
	}

	public function insert()
	{
		$username = $this->input->post('username');
		$password = $this->input->post('password');
		$repassword = $this->input->post('repassword');
		$power_id = $this->input->post('power_id');
		$exist = $this->db->where('username', $username)->count_all_results('webmasters');

		if ($exist) {
			ajax_return(1, '该帐号已被使用');
		}

		if ($password != $repassword) {
			ajax_return(1, '两次输入的密码不同');
		}

		$data = array('username' => $username, 'password' => $this->pwd->encrypt($password), 'power_id' => $power_id, 'add_time' => time());
		$rs = $this->db->insert('webmasters', $data);
		$this->ajax_result($rs, 'insert');
	}

	public function edit($id)
	{
		$id = intval($id);
		$rs = $this->db->where('id', $id)->get('webmasters');
		$d = $rs->row_array();
		$d['power'] = $this->d->rows(array('table' => 'power'));
		$this->load->view('webmasters_edit', $d);
	}

	public function update()
	{
		$id = $this->input->post('id');
		$username = $this->input->post('username');
		$password = $this->input->post('password');
		$repassword = $this->input->post('repassword');
		$power_id = $this->input->post('power_id');
		$user = $this->db->where('username', $username)->get('webmasters')->row_array();
		if ($user && ($user['id'] != $id)) {
			echo json_encode(array('msg' => '该帐号已被使用', 'error' => 1));
			exit();
		}

		if ($password != $repassword) {
			echo json_encode(array('msg' => '两次输入的密码不同', 'error' => 1));
			exit();
		}

		$data = array('username' => $username, 'password' => $this->pwd->encrypt($password), 'power_id' => $power_id);
		$rs = $this->db->where('id', $id)->update('webmasters', $data);
		$this->ajax_result($rs, 'update');
	}

	public function del()
	{
		$id = $this->input->get('id');

		if (!$id) {
			ajax_return(1, '找不到删除项');
		}

		$rs = $this->db->where_in('id', $id)->delete('webmasters');
		$this->ajax_result($rs, 'delete');
	}

	public function password()
	{
		$this->view('webmasters_password');
	}

	public function password_update()
	{
		$old_password = $this->input->post('old_password');
		$password = $this->input->post('password');
		$repassword = $this->input->post('repassword');
		$user = $this->db->where('id', $this->session->admin_id)->get('webmasters')->row_array();

		if (!$this->pwd->verify($old_password, $user['password'])) {
			ajax_return(1, '旧密码错误');
		}

		if ($password != $repassword) {
			ajax_return(1, '两次输入的密码不同');
		}

		$rs = $this->db->where('id', $user['id'])->set('password', $this->pwd->encrypt($password))->update('webmasters');
		$this->ajax_result($rs, 'update');
	}
}

?>
